<?php
require_once("dbtools.inc.php");
$id=$_COOKIE['id'];
$correct_t=$_COOKIE['correct'];

$link = create_connection();
$sql="select * from users where id=$id and status='normal'";
$result=@execute_sql($link,'zhihu',$sql);

$user = @mysqli_fetch_object($result)->username;
$user = $user.'thonsun123';
if($correct_t!=md5($user)){
    @mysqli_free_result($result);
    @mysqli_close($link);
    header("location:login.php");
}

$questions_id=$_GET['questionid'];

$sql ="update questions set look=look+1 where id=$questions_id";
$result=execute_sql($link,'zhihu',$sql);

$sql = "select * from questions where id=$questions_id";
$result = execute_sql($link,'zhihu',$sql);
$obj=@mysqli_fetch_object($result);

$title = $obj->title;
//var_dump($title,$questions_id);
$time=$obj->addtime;
$look=$obj->look;
$agree=$obj->agree;
$content=$obj->content;
$img=$obj->img;
$auth_id=$obj->users_id;

$sql = "select count(*) nums from answers where status='normal' and questions_id=$questions_id";
$result=execute_sql($link,'zhihu',$sql);
$answer_nums=@mysqli_fetch_object($result)->nums;

$sql="select count(*) nums from attentions where questions_id=$questions_id";
$result=execute_sql($link,'zhihu',$sql);
$guanzhu=@mysqli_fetch_object($result)->nums;

$sql="select username from users where id=$auth_id";
$result=execute_sql($link,'zhihu',$sql);
$auth=@mysqli_fetch_object($result)->username;

$path='images/'.$img.'.jpg';
if (isset($_GET['status']) && $_GET['questionid'] and $_GET['status']==0){
    $sql3 = "update invite set an_status=1 where invite_id=$id";
    execute_sql($link,'zhihu',$sql3);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Free Bootstrap Themes by Html5xCss3 dot com - Free Responsive Html5 Templates">
    <meta name="author" content="#">

    <title>问答社区|问题详情</title>

    <!-- SmartMenus jQuery Bootstrap Addon CSS -->
    <link href="css/menu.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="css/bootstrap/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="css/bootstrap/css/_bootswatch.scss">
    <link rel="stylesheet" type="text/css" href="css/bootstrap/css/_variables.scss">

    <!-- Custom CSS -->
    <link rel="stylesheet" type="text/css" href="css/style.css">

    <!-- Custom Fonts -->
    <link rel="stylesheet" href="font-awesome-4.4.0/css/font-awesome.min.css"  type="text/css">

    <link rel="stylesheet" href="css/lightbox.css">


    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
    <![endif]-->
    <script src="js/tinymce.min.js"></script>
    <script>tinymce.init({ selector:'textarea' ,language:'zh_CN'});</script>
    <style>
        .is-active{
            color: red;
        }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-lg navbar-dark" style="background:#C83935;">
    <div class="center">
        <div class="collapse navbar-collapse">
            <ul class="navbar-nav mr-auto">
                <li class="nav-item">
                    <a class="navbar-brand" href="index.php">TalkWith</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link active" href='index.php'><span class="fa fa-home" aria-hidden="true">首页</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href='submit_question.php'><span class="fa fa-pencil-square-o" aria-hidden="true">提问</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="profile.php"><span class="fa fa-bell-o" aria-hidden="true">通知</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="profile.php"><span class="fa fa-user" aria-hidden="true"></span>我的</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="logout.php"><span class="fa fa-power-off" aria-hidden="true">退出</span></a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div id="page-content" class="sub-page">
    <div class="container">
        <center>
        <article>
<?php
echo <<<EOT
<a class="example-image-link" href="$path" data-lightbox="example-set" data-title="Click the right half of the image to move forward."><img class="example-image" src="$path" alt=""/></a>
<div class="content-item">
    <h3 class="title-item"><a href="question_detail.php?questionid=$questions_id">$title</a></h3>
    <div class="time">$time</div>
    <ul class="list-inline">
        <li><a href="#"><i class="fa fa-eye"></i>$look</a></li>
        <li><a href="#"><i class="fa fa-comment"></i>$answer_nums</a></li>
        <li><a href="add_attention.php?questionid=$questions_id"><i class="fa fa-heart">$guanzhu</i></a></li>
        <li><a id="addlike-qst" onclick="addlike($id,$questions_id)" style="cursor:pointer;"><i id="is-active-qst$id" class="fa fa-thumbs-o-up"></i><i id="agreenum-qst$id">$agree</i></a></li>
    </ul>
    <p class="info">$content</p>
</div>


<div class="bottom-item">
    <a href="add_attention.php?questionid=$questions_id" class="btn btn-like"><i class="fa fa-heart-o"></i></a>
    <span class="user f-right">作者 $auth</span>
</div>
EOT;
?>

            <div id="contact_form">
            <?php
                echo "<form name='form1' id='ff' method='post' action='add_answer.php?questionid=$questions_id'>";
            ?>
                    <label>
                        <span>你的回答：</span>
                        <textarea name="content" id="message"></textarea>
                    </label>
                    <center><input class="sendButton" type="submit" name="submitcontact" value="提交"></center>
                </form>
            </div>
<div id="content_item">
<?php
    $sql="select * from answers where status='normal' and qst_id=$questions_id";
    $result=execute_sql($link,'zhihu',$sql);

    while($obj=mysqli_fetch_object($result)){
        $answers_id=$obj->id;
        $content=$obj->content;
        $user_id=$obj->users_id;
        $time=$obj->addtime;
        $agree=$obj->agree;

        $link2=create_connection();
        $sql2="select username from users where id=$user_id";
        $result2=execute_sql($link2,'zhihu',$sql2);
        $username=mysqli_fetch_object($result2)->username;

        $sql1="select * from comments where status='normal' and ans_id=$answers_id";
        $result1=execute_sql($link,'zhihu',$sql1);
        $sql1_1="select count(*) nums from comments where status='normal' and ans_id=$answers_id";
        $result1_1=execute_sql($link,'zhihu',$sql1_1);
        $comments_nums=mysqli_fetch_object($result1_1)->nums;

        $if_textarea = 1;

        echo <<<EOT
<div class="item">
    <div class="content-item">
        <strong class="title-item">$username</strong>
        <div class="time">$time</div>
        <p class="info">$content</p>
    </div>
    <div class="bottom-item">
        <a id="addlike-ans" onclick="add_ans_like($id,$answers_id,1)" style="cursor:pointer;" class="btn btn-like"><i id="is-active-ans$id" class="fa fa-thumbs-o-up"></i><i id="agreenum-ans$id">$agree</i></a>
        <a id="adddislike-ans" onclick="add_ans_dislike($id,$answers_id,0)" style="cursor:pointer;" class="btn btn-like"><i id="is-active-ans-dislike$id" class="fa fa-thumbs-o-down"></i><i id="agreenum-ans-dislike$id"></i></a>
        <a  href="javascript:divShow($answers_id)" id="btnhref$answers_id" style="cursor: pointer"><i class="fa fa-comment-o">$comments_nums</i>条评论</a>
        
EOT;
        if ($comments_nums == 0) {
            echo <<<EOT
            <div class="item" id="textarea-sub$answers_id"  style="display: none;padding: 15px;">
                <div class="content-item">
                    <form method='post' action='add_comment.php?answerid=$answers_id&questionid=$questions_id'>
                        <label>
                            <textarea name="comment_content" id="message" placeholder="写下你的评论..."></textarea>
                            <center><input class="sendButton" type="submit" name="submitcomment" value="发布"></center>
                        </label>
                    </form>
                </div>
            </div>
EOT;
        }
        echo <<<EOT
            <div  name="divshow$answers_id" style="display: none">
EOT;

        while ($obj1=mysqli_fetch_object($result1)) {
            $comments_id = $obj1->id;
            $users_id = $obj1->users_id;
            $comments_content = $obj1->content;
            $comments_agree = $obj1->agree;
            $comments_time = $obj1->addtime;

            $link3 = create_connection();
            $sql3 = "select username from users where id=$users_id";
            $result3 = execute_sql($link3, 'zhihu', $sql3);
            $usersname = mysqli_fetch_object($result3)->username;

        echo <<<EOT
        <!--   评论   -->
            <div class="item" style="padding: 15px;">
                <div class="content-item">
                    <strong class="title-item">$usersname</strong>
                    <div class="time">$comments_time</div>
                    <p class="info">$comments_content</p>
                    <div class="bottom-item">
                    <a id="addlike-com" onclick="add_com_like($id,$comments_id,1,$questions_id)" style="cursor:pointer;"><i id="is-active-com$id" class="fa fa-thumbs-o-up"></i><i id="agreenum-com$id">$comments_agree</i></a>
                    <a id="adddislike-com" onclick="add_com_dislike($id,$comments_id,0,$questions_id)" style="cursor:pointer;"><i id="is-active-com-dislike$id" class="fa fa-thumbs-o-down"></i><i id="agreenum-com-dislike$id"></i></a>
                    </div>
EOT;
                if ($if_textarea == $comments_nums) {
                echo <<<EOT
                    <form method='post' action='add_comment.php?answerid=$answers_id&questionid=$questions_id'>
                        <label>
                            <textarea name="comment_content" id="message" placeholder="写下你的评论..."></textarea>
                            <center><input class="sendButton" type="submit" name="submitcomment" value="发布"></center>
                        </label>
                    </form>
EOT;
                }
            echo <<<EOT
                </div>
            </div>
EOT;

        $if_textarea++;
            mysqli_free_result($result3);
            mysqli_close($link3);
        }
        echo <<<EOT
    </div>
    </div>
</div>
EOT;

            mysqli_free_result($result1);
            mysqli_free_result($result1_1);

            mysqli_free_result($result2);
            mysqli_close($link2);
    }
    mysqli_free_result($result);
    mysqli_close($link);
?>

            </div>

        </article>
        
        </center>
    </div>
</div>
<script type="text/javascript">
    function divShow(ansid){
        let div_array_show = document.getElementsByName("divshow"+ansid);
        for(let i = 0;i < div_array_show.length; i++){
            div_array_show[i].style.display = "block";
        }
        let textarea_sub = document.getElementById("textarea-sub"+ansid);
        textarea_sub.style.display = "block";
        console.log( document.getElementsByName("divshow"+ansid)[0])
        document.getElementById("btnhref"+ansid).href="javascript:divHidden("+ansid+")";
    }
    function divHidden(ansid){
        let div_array_hidden = document.getElementsByName("divshow"+ansid);
        for(let i = 0;i < div_array_hidden.length; i++){
            div_array_hidden[i].style.display = "none";
        }
        let textarea_sub = document.getElementById("textarea-sub"+ansid);
        textarea_sub.style.display = "none";
        document.getElementById("btnhref"+ansid).href="javascript:divShow("+ansid+")";
    }
</script>

<script type="text/javascript">
    function addlike(id,qid){
        htmlobj=$.ajax({
            url:"question_addlike.php",
            async:true,
            data:{'questionid':qid}
        });
        $("#agreenum-qst"+id).text(parseInt($("#agreenum-qst"+id).text())+1);
        $("#is-active-qst"+id).attr("class","fa fa-thumbs-o-up is-active");
    }


    function add_ans_like(id,sid,agree){
        htmlobj=$.ajax({
            url:"add_answer_like_dislike.php",
            async:true,
            data:{'answers_id':sid,'like':agree},
        });
        $("#agreenum-ans"+id).text(parseInt($("#agreenum-ans"+id).text())+1);
        $("#is-active-ans"+id).attr("class","fa fa-thumbs-o-up is-active");
    }
    function add_ans_dislike(id,sid,agree){
        htmlobj=$.ajax({
            url:"add_answer_like_dislike.php",
            async:true,
            data:{'answers_id':sid,'like':agree},
        });
        $("#is-active-ans-dislike"+id).attr("class","fa fa-thumbs-o-down is-active");
    }


    function add_com_like(id,cid,agree,questionid){
        htmlobj=$.ajax({
            url:"add_comment_like_dislike.php",
            async:true,
            data:{'comments_id':cid,'like':agree,'questionid':questionid},
        });
        $("#agreenum-com"+id).text(parseInt($("#agreenum-com"+id).text())+1);
        $("#is-active-com"+id).attr("class","fa fa-thumbs-o-up is-active");
    }
    function add_com_dislike(id,cid,agree,questionid){
        htmlobj=$.ajax({
            url:"add_comment_like_dislike.php",
            async:true,
            data:{'comments_id':cid,'like':agree,'questionid':questionid},
        });
        $("#is-active-com-dislike"+id).attr("class","fa fa-thumbs-o-down is-active");
    }
</script>
<!-- Once the page is loaded, initialized the plugin. -->
<script type="text/javascript" src="js/jquery-2.1.1.js" ></script>
<script src="js/bootstrap.min.js"></script>

<!-- SmartMenus jQuery Bootstrap Addon -->
<script type="text/javascript" src="js/jquery.smartmenus.bootstrap.js"></script>

<script src="js/lightbox-plus-jquery.min.js"></script>

<!-- Menu -->
<script src="js/script.js"></script>
</body>
</html>